package com.aorise.controller.assess;

import com.aorise.exception.ServiceException;
import com.aorise.model.assess.AssessModel;
import com.aorise.service.assess.AssessService;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.json.JsonResponseData;
import com.aorise.utils.page.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:cat
 * @Desicription: 考核管理控制器类
 * @Date:Created in 2019-06-20
 * @Modified By:
 */
@RestController
@Api(description ="考核管理接口")
public class AssessController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AssessService assessService;

    /**
     * 根据筛选条件进行分页查询考核信息
     * HTTP 方式：GET
     * API 路径：/api/assess/pageIndex/{pageIndex}/pageNum/{pageNum}
     * 方法名：getAllAssessListByPage
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-06-20
     */
    @ApiOperation(value = "根据筛选条件进行分页查询考核信息", httpMethod = "GET", response = String.class, notes = "根据筛选条件进行分页查询考核信息,提供考核管理-查询列表使用")
    @RequestMapping(value = "/api/assess/pageIndex/{pageIndex}/pageNum/{pageNum}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAllAssessListByPage(@ApiParam(value = "起始考核日期", required = false) @RequestParam(value = "startAssessDate", required = false) String startAssessDate,
                                         @ApiParam(value = "截止考核日期", required = false) @RequestParam(value = "endAssessDate", required = false) String endAssessDate,
                                         @ApiParam(value = "页索引", required = true) @PathVariable(value = "pageIndex", required = true) String pageIndex,
                                         @ApiParam(value = "页大小", required = true) @PathVariable(value = "pageNum", required = true) String pageNum) {
        logger.debug("Request RESTful API:getAllAssessListByPage");
        logger.debug("startAssessDate：" + startAssessDate);
        logger.debug("endAssessDate：" + endAssessDate);
        logger.debug("pageIndex" + pageIndex);
        logger.debug("pageNum" + pageNum);

        Page page = new Page();
        if (!StringUtils.isBlank(pageIndex)) {
            page.setCurrentPage(Long.parseLong(pageIndex.toString()));
        }
        if (!StringUtils.isBlank(pageNum)) {
            page.setEveryPage(Long.parseLong(pageNum.toString()));
        }
        try {

            Map<String, Object> map = new HashMap<>();
            map.put("startAssessDate", startAssessDate);
            map.put("endAssessDate", endAssessDate);
            map.put("beginIndex", page.getBeginIndex());
            map.put("endinIndex", page.getEndinIndex());

            List<AssessModel> assessModels = assessService.getAllAssessListByPage(map, page);
            page.setList(assessModels);

            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "", page).toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:AssessController. function:getAllAssessListByPage...msg:Get Assess List By Page DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), page).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:AssessController. function:getAllAssessListByPage...msg:Get Assess List By Page Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), page).toString();
        }
    }

    @ApiOperation(value = "测试", httpMethod = "GET", response = String.class, notes = "测试")
    @RequestMapping(value = "/api/assess/testAssess", method = RequestMethod.GET)
    public String testAssess() {
        assessService.updateAssess();
        return new JsonResponseData(true,
                StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS,
                "", null).toString();
    }

    /**
     * 每天0点5分统计前一天的考核管理信息
     * @Author: cat
     * @Date: 2019-06-20
     */
    //@Scheduled(cron = "0 */10 * * * ?")
    @Scheduled(cron = "0 5 0 * * ?")
    private void updateAssess(){
        try {
            logger.info("统计前一天的考核管理信息开始,当前时间为: {}",new Date());
            assessService.updateAssess();
            logger.info("统计前一天的考核管理信息结束,当前时间为: {}",new Date());
        } catch (ServiceException e) {
            logger.error(
                    "controller:AssessController. function:updateAssess...msg: Update Assess ServiceException");
            logger.error("error:" + e.getMessage());
        } catch (DataAccessException e) {
            logger.error(
                    "controller:AssessController. function:updateAssess...msg: Update Assess DataAccessException");
            logger.error("error:" + e.getMessage());
        } catch (Exception e) {
            logger.error(
                    "controller:AssessController. function:updateAssess. msg:Update Assess Exception");
            logger.error(e.getMessage());
        }
    }

    /**
     * 导出考核信息
     * HTTP 方式：GET
     * API 路径：/api/assess/exportAssess
     * 方法名：exportAssess
     * 方法返回类型：String
     *
     * @Author: cat
     * @Date: 2019-06-20
     */
    @ApiOperation(value = "导出考核信息", httpMethod = "GET", response = String.class, notes = "导出考核信息，提供考核管理-导出考核信息使用")
    @RequestMapping(value = "/api/assess/exportAssess", method = RequestMethod.GET)
    public String exportAssess(@ApiParam(value = "起始考核日期", required = false) @RequestParam(value = "startAssessDate", required = false) String startAssessDate,
                               @ApiParam(value = "截止考核日期", required = false) @RequestParam(value = "endAssessDate", required = false) String endAssessDate,
                               HttpServletResponse response) {
        logger.debug("Request RESTful API:exportAssess");
        logger.debug("startAssessDate：" + startAssessDate);
        logger.debug("endAssessDate：" + endAssessDate);
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("startAssessDate", startAssessDate);
            map.put("endAssessDate", endAssessDate);

            assessService.exportAssess(map, response);
            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS,
                    "", null).toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:AssessController. function:exportAssess...msg:Export Assess DataAccessException. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:AssessController. function:exportAssess...msg:Export Assess Exception. error:"
                            + e.getMessage());
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

}
